﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Tools
{
    /// <summary> 
    /// 正则表达式类，该类无法被继承且无法实例化
    /// </summary>
    sealed public class EasyRegex
    {
        private EasyRegex() { }

        private Regex regex;
        private MatchCollection matchCollection;

        /// <summary>
        /// 进行正则匹配
        /// </summary>
        /// <param name="pattern">正则表达式</param>
        /// <param name="input">被搜索的文本</param>
        /// <param name="ignoreCase">忽略大小写，默认：false 不忽略大小写。</param>
        /// <param name="multiline">是否多行匹配，默认：true，多行匹配</param>
        /// <returns></returns>
        public static EasyRegex Create(string pattern, string input, bool ignoreCase = false, bool multiline = true)
        {
            EasyRegex easyRegex = new EasyRegex();

            RegexOptions regexOptions = multiline ? RegexOptions.Multiline : RegexOptions.Singleline;
            if (ignoreCase)
                regexOptions = regexOptions | RegexOptions.IgnoreCase;
            try
            {
                easyRegex.regex = new Regex(pattern, regexOptions);
                easyRegex.matchCollection = easyRegex.regex.Matches(input);
            }
            catch (Exception)
            {

            }
            return easyRegex;
        }

        /// <summary>
        /// 取匹配数量
        /// </summary>
        /// <returns></returns>
        public int GetMatchesCount()
        {
            if (matchCollection == null)
                return 0;
            return matchCollection.Count;
        }

        /// <summary>
        /// 取匹配文本，如果匹配失败或下标超出范围则返回空文本
        /// </summary>
        /// <param name="index">下标，从0开始</param>
        /// <returns></returns>
        public string GetMatchesValue(int index = 0)
        {
            if (matchCollection == null)
                return "";
            if (matchCollection.Count == 0 || index < 0 || index >= matchCollection.Count)
                return "";
            return matchCollection[index].Value;
        }

        /// <summary>
        /// 取子匹配文本
        /// </summary>
        /// <param name="index">下标</param>
        /// <param name="group">子匹配索引，下标从0开始，0表示第一个“( )”的内容</param>
        /// <returns></returns>
        public string GetMatchesGroupsValue(int index = 0, int group = 0)
        {
            if (matchCollection == null)
                return "";
            if (matchCollection.Count == 0 || index < 0 || index >= matchCollection.Count)
                return "";
            if (group < 0 || group + 1 > GetGroupsCount())
                return "";

            return matchCollection[index].Groups[group + 1].Value;
        }

        /// <summary>
        /// 取子匹配数量，即正则表达式中“( )”的子匹配数量
        /// </summary>
        /// <returns></returns>
        public int GetGroupsCount()
        {
            if (matchCollection == null || matchCollection.Count == 0)
                return 0;

            return matchCollection[0].Groups.Count - 1;
        }
    }
}
